import { post, get, del, put } from '@/utils/http'
import { objToQueryString } from '@/utils/tools'

// 用户登录
export const doLoginApi = userData => {
  return post('/api/user/login', userData)
}

// 根据账号信息检查是否此账号是否存在
export const checkUserNameExistApi = username => {
  return post('/api/user/checkUserNameExist', { username })
}

// 新增用户
export const addUserApi = userData => {
  return post('/api/user/add', userData)
}

// 用户列表数据
export const getUsersApi = (
  page = 1,
  search = null,
  size = Number(process.env.VUE_APP_PAGESIZE)
) => {
  let url = `/api/user/list?page=${page}&size=${size}`
  if (search != null) {
    // {a:1,b:2} => a=1&b=2
    search.page && delete search.page
    url += objToQueryString(search)
  }

  return get(url)
}

// 根据用户id批量删除
export const deleteAllApi = ids => {
  return post(`/api/user/deleteAll`, { ids })
}

// 根据用户id删除指定用户
export const deleteUidToInfoApi = userid => {
  return del(`/api/user/del/${userid}`)
}

// 根据用户id返回对应记录数据
export const getUserIdToInfoApi = userid => {
  return get(`/api/user/info/${userid}`)
}

// 根据用户id修改对应用户记录数据
export const editUserIdToInfoApi = (userid, userData) => {
  return put(`/api/user/edit/${userid}`, userData)
}

// 根据用户id修改对应用户记录数据
export const uploadAvatarApi = formData => {
  return post(`/api/user/uploadAvatar`, formData)
}

// 根据用户id修改当前用户记录
export const editUserInfoApi = (id, userData) => {
  return put(`/api/user/edit/${id}`, userData)
}

// 下载excel文件
export const exportUsersExcelApi = () => {
  return get(`/api/user/exportUsersExcel`)
}

// 根据用户的角色id返回对应的菜单
export const getMenusApi = roleid => {
  return get(`/api/role/roleidToMenus/${roleid}`)
}
